package com.hzf.wxplatform.web.controller.manager.material;

import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.material.dao.model.MaterialShop;
import com.hzf.material.dao.model.MaterialShopExample;
import com.hzf.order.dao.model.OrderCart;
import com.hzf.order.dao.model.OrderCartExample;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/materialCart")
public class MaterialCartController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping("/index")
    private String index(Model model, HttpServletRequest request){
    /*    byte[] user = UcenterUserUtils.getUser(request.getSession());
        System.out.println(user);
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        OrderCartExample orderCartExample = new OrderCartExample();
        orderCartExample.createCriteria().andShopidEqualTo(materialShop.getShopId());
        List<OrderCart> list = apiService.selectByGoodsPrimaryKey(orderCartExample);
        model.addAttribute("list",list);
        return  thymeleaf("/CLS/CLS_Index");*/
        byte[] a = UcenterUserUtils.getUser(request.getSession());
        UcenterUser user = (UcenterUser) ByteUtils.ByteToObject(a); if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(user.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        OrderCartExample orderCartExample = new OrderCartExample();
        orderCartExample.createCriteria().andShopidEqualTo(materialShop.getShopId());
        List<OrderCart> list = apiService.selectByShopId(orderCartExample);
        for (OrderCart orderCart : list) {
            OrderCartExample orderCartExample1 = new OrderCartExample();
            orderCartExample1.createCriteria().andAShopidEqualTo(orderCart.getShopId());
            List<OrderCart> shopGoods = apiService.selectByGoodsPrimaryKey(orderCartExample1);
            for(OrderCart orderCart1:shopGoods){
                System.out.println(orderCart1.getInstallId());
            }
            orderCart.setGoods(shopGoods);
            orderCart.setShops(materialShop);
        }
        model.addAttribute("result", list);
        return  thymeleaf("/CLS/CLS_GWC_Shopping");
    }
}
